package com.easyssh.framework.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringUtil {
	private static final Logger logger = LoggerFactory.getLogger(StringUtil.class);
	
	public static String toUtf8Encode(String s) {
		String r = null;
		if(s != null) {
			try {
				r = URLEncoder.encode(s, "utf-8");
			} catch(Exception e) {
				logger.error("字符转换utf-8编码异常,${}",e);
			}
		}
		return r;
	}
	
	public static String toUtf8Decode(String s) {
		String r = null;
		if(s != null) {
			try {
				r = URLDecoder.decode(s, "utf-8");
			} catch(Exception e) {
				e.printStackTrace();
				logger.error("utf-8字符解码异常,${}",e);
			}
		}
		return r;
	}
	
	public static String md5(String toMd5){
		return toString(md5Private(toMd5.getBytes())).toUpperCase();
	}
	
	/**
	 * 进行md5运算
	 * @param cs
	 * @return
	 */
	private static byte[] md5Private(byte[] cs) {
		byte[] rs = null;
	      try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			rs = md.digest(cs);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} 
		return rs;
	}
	
	public static String toString(byte[] a) {
        if (a == null)
            return "null";
        if (a.length == 0)
            return "";
 
        StringBuilder buf = new StringBuilder();
 
        for (int i = 0; i < a.length; i++) {
        	if (a[i] < 0)
        		buf.append(Integer.toHexString(a[i]&0xff));
        	else if (a[i] < 16) {
        		buf.append('0');
        		buf.append(Integer.toHexString(a[i]));
        	} else {
        		buf.append(Integer.toHexString(a[i]));
        	}
        }
 
        return buf.toString();
    }
	
	public static String toIso8859_1(String fileName){
		if(StringUtils.isNotBlank(fileName)){
			try {
				return new String(fileName.getBytes(),"ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				logger.error("String toIso8859-1 error");
			}
		}
		return null;
	}
	
	/**
	 * 过滤html文本的html标签
	 * @param htmlText
	 * @return 过滤后的文本
	 */
	public static String filterHtmlTag(String htmlText){
		String regEx = "<.+?>"; // 表示标签
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(htmlText);
		String replace = matcher.replaceAll("");
		return replace;
	}
	
	/**
	 * 根据分页符分割文本
	 * @param content 需分割的文本
	 * @return 分割后文本数组
	 */
	public static String[] pagerContents(String content){
		String[] contents=null;
		//IE浏览器
		if(content.contains("<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>")){
			contents = content.split("<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>");
		//Firefox浏览器
		}else if(content.contains("<div style=\"page-break-after: always;\"><span style=\"display: none;\">&nbsp;</span></div>")){
			contents = content.split("<div style=\"page-break-after: always;\"><span style=\"display: none;\">&nbsp;</span></div>");
		}else{
			contents = content.split("<!-- pagebreak -->");
		}
		return contents;
	}
	
    public static String replaceHtml(String context){
    	context=context.replaceAll("\r\n", "<br/>");
    	context=context.replaceAll("\r", "<br/>");
    	context=context.replaceAll("\t", "&nbsp;");
    	return context;
    }
    
    public static void main(String[] args){
    	System.out.println(toUtf8Encode("肉"));
    }
}
